package no.ntnu.fp.model;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

import no.ntnu.fp.gui.EventDispatcher;
import no.ntnu.fp.model.events.NoticesChangedEvent;
import no.ntnu.fp.model.events.NoticesClearedEvent;

public class NoticeModel {
	private List<Notice> notices;

	public NoticeModel(){
		notices = new CopyOnWriteArrayList<Notice>();
	}

	public synchronized void setNotices(List<Notice> notices){
		NoticesChangedEvent evt = new NoticesChangedEvent(this, this.notices, notices);
		this.notices.clear();
		this.notices.addAll(notices);

		EventDispatcher.dispatchEvent(evt);                
	}
	public synchronized void addOrUpdateNotice(Notice notice){
		List<Notice> old = new ArrayList<Notice>(this.notices);
		this.notices.remove(notice);
		this.notices.add(notice);
		NoticesChangedEvent evt = new NoticesChangedEvent(this, old, this.notices);
		EventDispatcher.dispatchEvent(evt);
	}

	public void clearModel() {
		this.notices.clear();
		EventDispatcher.dispatchEvent(new NoticesClearedEvent(this));
	}
}
